const db = require('../db/db')
module.exports = {
    // 获取公告
    getNotices:async (ctx)=>{
        await db.promise().query('select * from notices').then(res=>{
            ctx.body = {
                code:1000,
                msg:'获取成功',
                notices:res[0]
            }
        }).catch(err=>{
            console.log(err);
            ctx.body = {
                code:10001,
                msg:'获取失败'+err,
            }
        })
    },
    // 获取用户总数逻辑
    getUsersNum:async (ctx)=>{
        await db.promise().query('select * from users').then(res=>{
            ctx.body = {
                code:1000,
                msg:'获取成功',
                usersNum:res[0].length
            }
        }).catch(err=>{
            console.log(err);
            ctx.body = {
                code:10001,
                msg:'获取失败'+err,
            }
        })
    },
    // 获取博客总数逻辑
    getBlogsNum:async (ctx)=>{
        await db.promise().query('select * from blogs').then(res=>{
            ctx.body = {
                code:1000,
                msg:'获取成功',
                blogsNum:res[0].length
            }
        }).catch(err=>{
            console.log(err);
            ctx.body = {
                code:10001,
                msg:'获取失败'+err,
            }
        })
    },
    // 获取网站访问量
    getVisitorNum:async (ctx)=>{
        await db.promise().query('select * from visitornum').then(async res=>{
            ctx.body = {
                code:1000,
                msg:'获取成功',
                visitorNum:res[0][0].visitorNum
            }
            const visitorNum = res[0][0].visitorNum + 1
            await db.promise().query('update visitornum set visitorNum=?',[visitorNum]).then(res=>{

            }).catch(err=>{
                ctx.body = {
                    code:10002,
                    msg:'访问量出现错误'+err,
                }
            })
        }).catch(err=>{
            ctx.body = {
                code:10001,
                msg:'获取失败'+err,
            }
        })
    },
    // 获取用户占比
    getUsersMix:async (ctx)=>{
        let rootNum, adminNum, userNum, demoNum, testNum
        await db.promise().query('select identity from users where identity=0').then(res=>{
            rootNum = res[0].length
        }).catch(err=>{
            ctx.body = {
                code:1001,
                msg:'获取失败'+err
            }
        })
        await db.promise().query('select identity from users where identity=1').then(res=>{
            adminNum = res[0].length
        }).catch(err=>{
            ctx.body = {
                code:1001,
                msg:'获取失败'+err
            }
        })
        await db.promise().query('select identity from users where identity=2').then(res=>{
            userNum = res[0].length
        }).catch(err=>{
            ctx.body = {
                code:1001,
                msg:'获取失败'+err
            }
        })
        await db.promise().query('select identity from users where identity=3').then(res=>{
            demoNum = res[0].length
        }).catch(err=>{
            ctx.body = {
                code:1001,
                msg:'获取失败'+err
            }
        })
        await db.promise().query('select identity from users where identity=3').then(res=>{
            testNum = res[0].length
        }).catch(err=>{
            ctx.body = {
                code:1001,
                msg:'获取失败'+err
            }
        })
        ctx.body = {
            code:1000,
            msg:'获取成功',
            userMix:{
                rootNum:rootNum,
            adminNum:adminNum,
            userNum:userNum,
            demoNum:demoNum,
            testNum:testNum,
            }
        }

    }
}
